package com.bondqin.demo.action;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bondqin.demo.dao.repo.AnnualPlanRepo;
import com.bondqin.demo.entity.AnnualPlan;
import com.bondqin.demo.util.ModelUtil;
import com.bondqin.demo.util.ResultModel;
import com.bondqin.demo.vo.AnnualPlanVo;

/**
 * 医院年度计划
 * @author bond
 *
 */
@Service
public class AnnualPlanAction {

	@Autowired
	private AnnualPlanRepo personRepo;

	/**
	 * 查询列表
	 * @param number	页数
	 * @param size		每页个数
	 * @return
	 */
	public Map<String, Object> listAll(Integer number, Integer size) {
		Map<String, Object> model = new HashMap<String, Object>();
		
		Pageable pageable = new PageRequest(number, size);
		Page<AnnualPlan> page = personRepo.findAll(pageable);
		
		Page<AnnualPlanVo> pageVo = ModelUtil.toPage(page, AnnualPlanVo.class);
		
		model.put("page", pageVo);
		return model;
	}

	/**
	 * 新增
	 * @param person
	 * @return
	 */
	@Transactional
	public ResultModel add(AnnualPlanVo record) {
		ResultModel result = new ResultModel();
		
		AnnualPlan entity = new AnnualPlan();
		BeanUtils.copyProperties(record, entity);

		personRepo.save(entity);
		
		result.setSuccess(true);
		return result;
	}
	
	/**
	 * 修改
	 * @param person
	 * @return
	 */
	@Transactional
	public ResultModel modify(AnnualPlanVo record) {
		ResultModel result = new ResultModel();
		
		if (record.getPlanID() == null) {
			result.setError("ID不能为空");
			return result;
		}
		
		AnnualPlan entity = new AnnualPlan();
		BeanUtils.copyProperties(record, entity);

		personRepo.save(entity);
		
		result.setSuccess(true);
		return result;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Transactional
	public ResultModel remove(Integer id) {
		ResultModel result = new ResultModel();
		
		if (id == null) {
			result.setError("ID不能为空");
			return result;
		}
		
		personRepo.delete(id);
		
		result.setSuccess(true);
		return result;
        
	}
}